<?php
/**
 * 2016.8.9 作业题
 * 分割字符串用逗号隔开
 * User: wuz
 * Date: 2016/8/9
 * Time: 11:10
 */

/*2016.8.9作业
 *String的运用的集合类
 * */
class StringExample
{
    //第一题的1112223334445 逗号隔开实现函数
    public function strExchange($str)
    {
        $array = $this->substrMethod($str, 0, 3); //调用substrMethod方法切割字符串
        $str   = implode(",", $array);
        return strrev($str);
    }

    //将字符串按指定长度分割 并存在一维数组中
    public function substrMethod($str, $star, $length)
    {
        for ($i = $star; $i < strlen($str); $i += $length) {
            $substrArray[$i] = mb_substr($str, $i, $length);
        }
        return array_values($substrArray);
    }

    //第二题的www.yaochufa.com实现函数
    public function cutAndStrrev($cutAndStrrev)
    {
        $cutAndStrrev = strstr($cutAndStrrev, ".");
        return strrev($cutAndStrrev);
    }

    //切割成单词并存数组中  第三第五题使用切割
    public function cutWord($str)
    {
        return str_word_count($str, 1);
    }

    //第三题  将带有间隔符的单词串转成首字母大写的字符串
    public function removeAndUpper($str)
    {
        $array = $this->cutWord($str);
        for ($i = 0; $i < count($array); $i++) {
            $temp[$i] = ucfirst($array[$i]); //将首字母大写
        }
        return implode("", $temp); //合并单词成串
    }

    //第四题
    public function pickUpOne($str)
    {
        return strstr($str, "y");
    }
    public function pickUpTwo($str)
    {
        return substr($str, 7);
    }
    public function pickUpThree($str)
    {
        return str_replace("liming@", "", $str);
    }
    public function pickUpFour($str)
    {
        $str = explode("@", $str);
        return $str[1];
    }
    //第五题 翻转字符串中的单词
    public function turnWord($str)
    {
        $turnWord = "";
        $array    = $this->cutWord($str); //调用切割单词的方法
        for ($i = count($array) - 1; $i >= 0; $i--) {
            $turnWord = $turnWord . " " . $array[$i]; //从数组中反向取出单词组成串
        }
        return $turnWord;
    }

    //附加题
    public function title($str, $start)
    {
        // $a = str_split($str, 30);
        $title = $this->substrMethod($str, $start, 60); //utf-8 一个汉字3个字节
        return $title[0] . "...";
    }

}
